package www.ld.com.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import www.ld.com.core.group.InsertGroup;
import www.ld.com.core.group.UpdateGroup;
import www.ld.com.domain.R;
import www.ld.com.domain.roles.pojo.RolesPO;
import www.ld.com.domain.roles.query.RolesQuery;
import www.ld.com.domain.roles.vo.InRolesVO;
import www.ld.com.domain.roles.vo.OutRolesVO;
import www.ld.com.domain.roles.vo.RolesVO;
import www.ld.com.domain.vo.PageRestVO;
import www.ld.com.domain.vo.SelectVO;
import www.ld.com.service.RolesService;
import www.ld.com.utils.PageUtils;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("roles")
@Api(tags = "角色模块")
@Slf4j
public class RolesController {
    @Autowired
    private RolesService rolesService;

    @ApiOperation(value = "查询角色列表")
    @GetMapping
    @PreAuthorize("hasAuthority('sys:roles:select')")
    public R<PageRestVO<RolesVO>> pageList(RolesQuery rolesQuery) {
        PageRestVO pageRestVO = PageUtils.copyPageVO(rolesService.pageRolesByPostQuery(rolesQuery), RolesVO.class);
        return R.ok(pageRestVO);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取角色详情")
    @PreAuthorize("hasAuthority('sys:roles:select')")
    public R<OutRolesVO> getRolesById(@PathVariable(value = "id", required = true) String id) {
        return R.ok(rolesService.getOutRolesVOById(id));
    }

    @ApiOperation(value = "新增角色")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:roles:create')")
    public R save(@RequestBody @Validated(value = {InsertGroup.class}) InRolesVO inRolesVO) {
        rolesService.saveRolesPO(BeanUtil.toBean(inRolesVO, RolesPO.class),  inRolesVO.getResources());
        return R.ok();
    }

    @ApiOperation(value = "修改岗位")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:roles:update')")
    public R update(@RequestBody @Validated(value = {UpdateGroup.class}) InRolesVO inRolesVO) {
        rolesService.updateRolesPOById(BeanUtil.toBean(inRolesVO, RolesPO.class),  inRolesVO.getResources());
        return R.ok();
    }

    @ApiOperation(value = "删除岗位")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:roles:delete')")
    public R delete(@PathVariable(value = "id", required = true) String id) {
        rolesService.deleteRolesById(id);
        return R.ok();
    }
    @ApiOperation(value = "查询角色")
    @GetMapping("/find")
    public R<SelectVO> findRoles() {
        List<RolesPO> rolesPO = rolesService.findRolesPO();
        List<SelectVO>  selects= rolesPO.stream().map(v ->
                BeanUtil.toBean(v, SelectVO.class)
        ).collect(Collectors.toList());
        return R.ok( selects);
    }
}
